package com.dao;

import com.pojo.NurseContent;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class NurseContentDao extends BaseDao<NurseContent>{
    @Override
    public NurseContent rowMapper(ResultSet rs) {
        NurseContent content = new NurseContent();
        try {
            content.setId(rs.getInt("id"));
            content.setSerialNumber(rs.getString("serial_number"));
            content.setNursingName(rs.getString("nursing_name"));
            content.setServicePrice(rs.getString("service_price"));
            content.setMessage(rs.getString("message"));
            content.setStatus(rs.getInt("status"));
            content.setExectionCycle(rs.getString("execution_cycle"));
            content.setExectionTime(rs.getString("execution_times"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return content;
    }

    public int addNurseContent(NurseContent content) {
        String sql = "INSERT INTO nursecontent(serial_number,nursing_name,service_price,message,status,execution_cycle,execution_times,is_deleted)VALUES(?,?,?,?,?,?,?,0)";
        Object[] arr = {content.getSerialNumber(),
        content.getNursingName(),content.getServicePrice(),content.getMessage(),
        content.getStatus(),content.getExectionCycle(),content.getExectionTime()};
        if (this.update(sql,arr))
            return 1;
        else return 0;
    }

    public List<NurseContent> searchAll() {
        String sql = "SELECT id,serial_number,nursing_name,service_price,message,status,execution_cycle,execution_times FROM nursecontent WHERE is_deleted = 0 OR is_deleted is null";
        return this.findMultiple(sql,null);
    }

    public int updateNurseContent(NurseContent content) {
        String sql = "UPDATE nursecontent set serial_number = ?,nursing_name = ?,service_price = ?,message = ?,status = ?,execution_cycle = ?,execution_times = ?" +
                " WHERE id = ?";
        Object[] arr = {content.getSerialNumber(),
                content.getNursingName(),content.getServicePrice(),content.getMessage(),
                content.getStatus(),content.getExectionCycle(),content.getExectionTime(),content.getId()};
        if (this.update(sql,arr))
            return 1;
        else return 0;
    }

    public int deleteNurseContent(int id) {
        String sql = "UPDATE nursecontent set is_deleted = 1 WHERE id = ?";
        Object[] arr = {id};
        if (this.update(sql,arr))
            return 1;
        else return 0;
    }
}
